package com.example.multithread;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreTest {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2);
        for (int i = 0; i < 5; i++) {
            new Thread(() ->{
                try {
                    semaphore.acquire();
                    if (semaphore.availablePermits() >= 0) {
                        System.out.printf("【业务办理 - 开始】当前办理业务人员的信息为: %s %n", Thread.currentThread().getName());
                        TimeUnit.SECONDS.sleep(2);
                        System.out.printf("【业务办理 - 结束】当前办理业务人员的信息为: %s %n", Thread.currentThread().getName());
                        semaphore.release();
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }, "用户-" + i).start();
        }
    }
}
